// pages/Goods/goodsOrder/index.js
const common = require("../../../../public.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    relationContent:'',
    relationImage:'',
      currentItem:{},
    zeroShow:false,
    userInfo: {},
    token: '',
    orderList: [],
    status: 0,
    refreshing: false,
    noMoreData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token,
          openid: data.openid
        }, function () {
          that.loadTabData()
        })
      }
    })
  },
  onChange(event) {
    const index = event.detail.index;
    this.data.status = index<3?index:index+1;
    this.loadTabData()
  },
  getOrderList(refresh = false) {
    const that = this
    if (this.data.isLoadingBusy === true) {
      return
    }
    if (refresh) {
      this.setData({
        noMoreData: false,
        refreshing: true,
        isLoadingBusy: true
      })
    } else {
      if (this.data.noMoreData === true) {
        return
      }
      this.setData({
        isLoadingBusy: true,
        refreshing: false
      })
    }

    const pageNum = refresh ? 1 : this.data.pageNum + 1
    common.post('/findListByUserId',
      {
        userId: that.data.userInfo.id,
        status: that.data.status,
        token: that.data.token,
        pageNum
      },
      function (res) {
        if (res.status == 200) {
          that.data.pageNum = pageNum
          that.setData({
            orderList: refresh ? res.data.result_list : [...that.data.orderList, ...res.data.result_list]
          })

          if (pageNum >= res.data.totalPage) {
            that.setData({
              noMoreData: true
            })
          }
        } else {
          common.showRequestErrMsg(res)
        }
      },
      function () {
        that.setData({
          isLoadingBusy: false
        })
        if (refresh) {
          // 增加延迟标记刷新完成，防止scroll-view多次加载刷新
          that.setData({
            refreshing: false
          })
        }
      }
    )
  },
  loadMore() {
    this.getOrderList(false)
  },
  /**
   * 重新加载当前tab页数据，处理下拉刷新
   */
  reloadData() {
    this.getOrderList(true)
  },
  /**
   * 加载tab页数据，不显示下拉刷新状态
   */
  loadTabData() {
    this.setData({
      orderList: []
    }, _ => {
      this.getOrderList(true)
    })
  },
  toStore(e) { },
  payNow(e) {
    const item = e.currentTarget.dataset.item
    const that = this
    const tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      orderId: item.id,
      openid: that.data.openid,
    }
    common.wxPayRequest("/wxxcxPayForZeroGoods", tempJson, function (res) {
      if (res.status === 200) {
        //微信支付
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            common.showToast('支付成功', 'success')
            that.loadTabData()
          },
          'fail': function (res) {
            if (res.errMsg === 'requestPayment:fail cancel') {
            } else {
              common.showToast(res.errMsg)
            }
          }
        })
      }
    })
  },
  confirm(e) {
    const item = e.currentTarget.dataset.item
    const that = this
    const params = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      orderNo: item.orderNo
    }
    common.get('/confirmTheGoods', params, function (res) {
      if (res.status === 200) {
        that.loadTabData()
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  cancelOrder(e) {
    const that = this
    common.showAlertView('提示', '是否取消该订单', function (res) {
      if (res) {
        const orderNo = e.currentTarget.dataset.item.orderNo
        const tempJson = {
          userId: that.data.userInfo.id,
          token: that.data.token,
          orderNo
        }
        common.post("/closeZerobuyOrder", tempJson, function (res) {
          if (res.status === 200) {
            common.showToast('已取消订单')
            that.loadTabData()
          } else {
            common.showRequestErrMsg(res)
          }
        })
      }
    })
  },
  // 删除按钮点击事件
  onDelete(e) {
    const orderId = e.currentTarget.dataset.id
    const that = this
    common.showAlertView('提示', '是否删除该订单？', function (res) {
      if (res) {
        that.submitDelete(orderId)
      }
    })
  },
  // 提交删除事件
  submitDelete(orderId) {
    const that = this
    common.post('/deleteZerobuyOrder', {
      orderId,
      userId: that.data.userInfo.id,
      token: that.data.token
    },
      function (res) {
        if (res.status == 200) {
          common.showToast('删除成功！')
          that.loadTabData()
        } else {
          common.showRequestErrMsg(res)
        }
      })
  },
  getRegistFeeFunc(e) {

    this.getGoodsDes(e);

  },

  getGoodsDes(e) {
    const that = this
    that.setData({
      isLoadingBusy: true
    })
    common.getJson(
      '/getZeroGoodsRelationInfo',
      {
        zerobuyId: e.currentTarget.dataset.item.zerobuyId
      },
      function (res) {
          console.log('getZeroGoodsRelationInfo---',res);
        if (res.status === 200 && res.data) {
            that.setData({
                zeroShow:true,
                relationContent:res.data.relationContent,
                relationImage:res.data.relationImage,
                currentItem:e.currentTarget.dataset.item,
            })
            // const item = e.currentTarget.dataset.item
            // console.log('领取返现',item);
        } else {
          common.showRequestErrMsg(res)
        }
      },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      }
    )
  },


  toDetail(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
    //   url: '/packageC/pages/zeroOrderDes/index?orderNo=' + item.orderNo,
    url: '../zeroOrderDes/index?orderNo=' + item.orderNo,
    })
  },


  onClickHide(){
    this.setData({
        zeroShow: false
    })
  },
  closeTap() {
    this.setData({
        zeroShow: false
    })
},
getMoneyFunc() {
    console.log('领取返现点击',this.data.currentItem);

   
    wx.navigateTo({
    //   url: '/packageC/pages/zeroOrderDes/index?orderNo=' + item.orderNo,
    url: '/pages/dayCash/index?orderNo=' + this.data.currentItem.orderNo,
    })
  },
})